<template>
  <div ref="wrapper">
    <div>
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";

export default {
  name: "scroller",
  data() {
    return {
      scroller: {
        type: Object,
        default() {
          return {};
        },
      },
    };
  },
  //接收父组件来的值是否监听滚动
  props: {
    probeType: {
      type: Number,
      default: 0,
    },
    pullUpload: {
      type: Boolean,
      default: false,
    },
  },
  mounted() {
    this.scroller = new BScroll(this.$refs.wrapper, {
      probeType: this.probeType, //当父组件传值probeType为3 可以监听位置
      pullUpLoad: this.pullUpload,  //当pullUpLoad为true 执行加载更多函数
      click: true,
      eventPassthrough: "horizontal", 
      useTransition: false //切换为js true是css3的移动快速点击滑动会抖动
    });
    /**监听滚动位置 */
    if (this.probeType === 2 || this.probeType === 3) {
      this.scroller.on("scroll", (position) => {
        //console.log(postion);
        this.$emit("scroll", position);
      });
    }
    /**下拉加载更多 */
    if (this.pullUpload) {
      this.scroller.on('pullingUp',()=>{
        this.$emit('pullingUp')
      })
    }
  },
  methods: {
    // 返回顶部
    scrollTo(x, y, time = 300) { 
      this.scroller && this.scroller.scrollTo(0, 0, time);
    },
    // 刷新方法
    refresh() {
      this.scroller && this.scroller.refresh();
    },
    // 下拉加载更多
    finishPullUp() { 
      this.scroller && this.scroller.finishPullUp();
    },
    // 获取到当时的y坐标
    getCurrentY() {
      return this.scroller.y ? this.scroller.y : 0;
    }
  },
};
</script>

<style scoped>
</style>
